package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"

	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common"
)

var (
// tokenNotExist = errors.New("token not exist")
)

type BaseController struct{}

//
// 获取token
//
//func (c *BaseController) GetToken(ctx *gin.Context) (string, error) {
//	authTokenContext, ok := ctx.Get(constants.CONTEXT_AUTH_TOKEN_KEY)
//	if !ok {
//		return "", tokenNotExist
//	}
//
//	authTokenStr, ok := authTokenContext.(string)
//	if !ok {
//		return "", tokenNotExist
//	}
//	return authTokenStr, nil
//}

// Success
// 成果返回数据
func (c *BaseController) Success(ctx *gin.Context, data interface{}) {
	result := gin.H{
		"success":    true,
		"message":    "操作成功",
		"error_code": 0,
		"data":       data,
	}

	ctx.JSON(http.StatusOK, result)
}

// Fail
// 失败返回数据数据
func (c *BaseController) Fail(ctx *gin.Context, code int, msg string) {
	result := map[string]interface{}{
		"success":    false,
		"message":    common.WrapError(msg),
		"error_code": code,
		"data":       nil,
	}

	ctx.JSON(http.StatusOK, result)
}

// FailWithData 失败返回数据
func (c *BaseController) FailWithData(ctx *gin.Context, code int, msg string, data interface{}) {
	result := map[string]interface{}{
		"success":    false,
		"message":    common.WrapError(msg),
		"error_code": code,
		"data":       data,
	}

	ctx.JSON(http.StatusOK, result)
}

type HealthController struct {
	BaseController
}

func (c *HealthController) RegisterRouter(g *gin.RouterGroup) {
	//心跳检测
	g.Any("/healthz", c.Health)
}

func (c *HealthController) Health(ctx *gin.Context) {
	c.Success(ctx, map[string]interface{}{
		"health": "健康检测ok(正常)",
	})
}
